﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DAL;
using DAL.Models;
using BAL;
using BAL.Interfaces;
using BAL.Infrastructure;

namespace BibliotecaVirtual.Controllers
{
    [Authorize(Roles = Libreria.Constantes.ADMINISTRADOR)]
    public class TemasMaterialsController : Controller
    {
        private ITemasMaterialesService BALTemasMateriales = null;
        private ITemasService BALTemas = null;
        private IMaterialsService BALMaterials = null;

        public TemasMaterialsController(ITemasMaterialesService TemasMaterialesService,
                                   ITemasService TemasService,
                                   IMaterialsService MaterialsService)
        {
            BALTemasMateriales = TemasMaterialesService;
            BALTemas = TemasService;
            BALMaterials = MaterialsService;
        }

        //
        // GET: /TipoMaterial/

        public ViewResult Index()
        {
            return View(BALTemasMateriales.GetAll());
        }

        //
        // GET: /TipoMaterial/Details/5

        public ViewResult Details(long id)
        {
            object[] keys = { id };
            TemasMateriales item = BALTemasMateriales.Find(keys);
            return View(item);
        }

        //
        // GET: /TipoMaterial/Create

        public ActionResult Create()
        {
            cargarDatos(null);
            return View();
        } 

        //
        // POST: /TipoMaterial/Create

        [HttpPost]
        public ActionResult Create(TemasMateriales item)
        {
            if (ModelState.IsValid)
            {
                BALTemasMateriales.Create(item);
                return RedirectToAction("Index");  
            }
            cargarDatos(item);
            return View(item);
        }
        
        //
        // GET: /TipoMaterial/Edit/5
 
        public ActionResult Edit(long id)
        {
            object[] keys = { id };
            TemasMateriales item = BALTemasMateriales.Find(keys);
            cargarDatos(item);
            return View(item);
        }

        //
        // POST: /TipoMaterial/Edit/5

        [HttpPost]
        public ActionResult Edit(TemasMateriales item)
        {
            if (ModelState.IsValid)
            {
                BALTemasMateriales.Edit(item);
                return RedirectToAction("Index");
            }
            cargarDatos(item);
            return View(item);
        }

        //
        // GET: /TipoMaterial/Delete/5
 
        public ActionResult Delete(long id)
        {
            object[] keys = { id };
            TemasMateriales item = BALTemasMateriales.Find(keys);
            return View(item);
        }

        //
        // POST: /TipoMaterial/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {
            object[] keys = { id };
            TemasMateriales item = BALTemasMateriales.Find(keys);
            BALTemasMateriales.Remove(item);
            return RedirectToAction("Index");
        }

        //protected override void Dispose(bool disposing)
        //{
        //    BALTemasMateriales.Dispose();
        //    base.Dispose(disposing);
        //}

        protected void cargarDatos(TemasMateriales item)
        {
            try
            {
                if (item != null)
                {
                    ViewBag.TemaId = new SelectList(BALTemas.GetAll(), "id", "Nombre", item.TemaId);
                    ViewBag.MaterialId = new SelectList(BALMaterials.GetAll(), "id", "Titulo", item.MaterialId);
                }
                else
                {
                    ViewBag.TemaId = new SelectList(BALTemas.GetAll(), "id", "Nombre");
                    ViewBag.MaterialId = new SelectList(BALMaterials.GetAll(), "id", "Titulo");
                }
            }
            catch (Exception ex)
            {

            }
        }
    }
}